/* 使用栈计算逆波兰式 */

#include<stack>
#include<iostream>
#include<sstream>
#include<string>
#include<cassert>
using namespace std;


int CalcReversePolish(const char* str)
{
	istringstream istr(str);
	stack<int> stData;

	string strSub;

	while (istr >> strSub)
	{
		if (strSub == "+")
		{
			int a = stData.top();
			stData.pop();
			int b = stData.top();
			stData.pop();
			stData.push(a + b);
		}
		else if (strSub == "-")
		{
			int a = stData.top();
                        stData.pop();
                        int b = stData.top();
                        stData.pop();
                        stData.push(b - a);
		}
		else if (strSub == "*")
		{
			int a = stData.top();
                        stData.pop();
                        int b = stData.top();
                        stData.pop();
                        stData.push(a * b);
		}
		else if (strSub == "/")
		{
			int a = stData.top();
                        stData.pop();
                        int b = stData.top();
                        stData.pop();
                        stData.push(a / b);
		}
		else
		{
			stData.push(atoi(strSub.c_str()));
		}
	}

	int value = stData.top();
	stData.pop();
	assert(stData.empty());

	return value;
}

int main(void)
{
	cout<< CalcReversePolish("1 2 + 3 4 - *") <<endl;
}
